package com.xiong.xtools.myguide;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.xiong.xtools.R;
/**
 * Author : 熊文强
 * Time : 2017/9/19
 * Content : 自定义颜色渐变引导页
 */
public class GuideActivity extends FragmentActivity {
	private static final int[] resource = new int[]{
			R.mipmap.welcome1, R.mipmap.welcome4,
			R.mipmap.welcome3, R.mipmap.welcome4};
	private static final String TAG = GuideActivity.class.getSimpleName();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_guide);
		MyFragmentStatePager adpter = new MyFragmentStatePager(getSupportFragmentManager());
		ColorAnimationView colorAnimationView = (ColorAnimationView) findViewById(R.id.ColorAnimationView);
		ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
		viewPager.setAdapter(adpter);
		/**
		 *  首先，你必须在 设置 Viewpager的 adapter 之后在调用这个方法
		 *  第二点，setmViewPager(ViewPager mViewPager,Object obj, int count, int... colors)
		 *         第一个参数 是 你需要传人的 viewpager
		 *         第二个参数 是 一个实现了ColorAnimationView.OnPageChangeListener接口的Object,用来实现回调
		 *         第三个参数 是 viewpager 的 孩子数量
		 *         第四个参数 int... colors ，你需要设置的颜色变化值~~ 如何你传人 空，那么触发默认设置的颜色动画
		 * */
		colorAnimationView.setmViewPager(viewPager, resource.length);
		colorAnimationView.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
				Log.d("TAG","onPageScrolled");
			}

			@Override
			public void onPageSelected(int position) {
				Log.d("TAG","onPageSelected");
			}

			@Override
			public void onPageScrollStateChanged(int state) {
				Log.d("TAG","onPageScrollStateChanged");
			}
		});
	}



	public class MyFragmentStatePager extends FragmentStatePagerAdapter {

		public MyFragmentStatePager(FragmentManager fm) {
			super(fm);
		}

		@Override public Fragment getItem(int position) {
			return new MyFragment(position);
		}

		@Override public int getCount() {
			return resource.length;
		}
	}

	@SuppressLint("ValidFragment") public static class MyFragment extends Fragment {
		private int position;

		public MyFragment(int position) {
			this.position = position;
		}

		@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
			ImageView imageView = new ImageView(getActivity());
			imageView.setImageResource(resource[position]);
			return imageView;
		}
	}
}
